﻿using Catastrophe.BasePC.UI.UIMode;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Catastrophe.BasePC.UI.UI
{
    /// <summary>
    /// 菜单管理
    /// </summary>
    public partial class UcSysMenu : UserControl
    {
        UcSysMenuModel model;
        public UcSysMenu()
        {
            InitializeComponent();
            model = this.DataContext as UcSysMenuModel;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_add_Click(object sender, RoutedEventArgs e)
        {
            var uc = CreateSysMenuEdit();
            uc.model.FormName = "添加菜单";
            uc.ShowDialog();
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_edit_Click(object sender, RoutedEventArgs e)
        {
            var select = FindChecked();
            if (select == null)
            {
                return;
            }
            var uc = CreateSysMenuEdit();
            uc.model.FormName = "编辑菜单";
            uc.model.Assembly = select.Assembly;
            uc.model.Resource_name = select.ResourceName;
            uc.model.dataId = select.Id;
            uc.model.Name = select.NodeName;
            uc.model.Index = select.BaseIndex;
            uc.model.Visible = select.Visible;
            uc.model.Is_fixed = select.IsFixed;
            uc.model.Instance_life = select.InstanceLife;
            uc.model.Soft = select.SoftId;
            foreach (var item in uc.model.NodeItems)
            {
                if (select.ParentIndex==item.index)
                {
                    uc.model.SelectnodeItems = item;
                }
            }
            uc.ShowDialog();
        }
        private SysMenuItem FindChecked()
        {
            foreach (var item in model.SysMenuEntities)
            {
                if (item.IsCheck)
                {
                    return item;
                }
            }
            return null;
        }
        private UcSysMenuEdit CreateSysMenuEdit()
        {
            UcSysMenuEdit uc = new UcSysMenuEdit();
            uc.OkClick += Uc_OkClick;
            foreach (var item in model.SysMenuEntities)
            {
                if (item.ParentIndex == -1)
                {
                    uc.model.NodeItems.Add(new Model.NodeItem()
                    {
                        index = item.BaseIndex,
                        name = item.NodeName,
                    });
                }
            }
            return uc;
        }

        private void Uc_OkClick(bool obj)
        {
            if (obj)
            {
                model.LoadMenu();
            }
        }
    }
}
